#include "TabWidget.h"



#include <ItemModel.h>
#include <CampaignItem.h>
#include <FileHandler.h>



#include <QTabBar>
#include <QLineEdit>
#include <QTreeView>
#include <QHeaderView>
#include <QMessageBox>
#include <QFile>
#include <QFileDialog>
#include <QTimer>



TabWidget::TabWidget(QWidget *parent) :
    QTabWidget(parent)
{
    edit_index = -1;
}



void TabWidget::onNew(CampaignItem *item)
{
    if (!item) item = CampaignItem::add("New Campaign");

    connect(item, SIGNAL(sigDirty()), this, SLOT(onRefresh()));

    QTreeView* view = new QTreeView(this);
    view->setModel(item->model());

    addTab(view, item->name());
    setCurrentWidget(view);
}



void TabWidget::onOpen()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open"), QDir::currentPath(), tr("Xml Files (*.xml)"));

    if (fileName.isEmpty()) return;

    CampaignItem* item = qobject_cast<CampaignItem*>(FileHandler::open(fileName));
    if (!item)
    {
        QMessageBox::information(0, tr("Open Failed"), tr("Failed to open campaign!"));
        return;
    }
    item->setFilePath(fileName);
    item->setDirty(false);

    onNew(item);
}



void TabWidget::onSave(int index)
{
    if (index < 0) index = currentIndex();

    QTreeView* view = qobject_cast<QTreeView*>(widget(index));
    if (!view) return;

    ItemModel* model = qobject_cast<ItemModel*>(view->model());
    if (!model) return;


    CampaignItem* item = qobject_cast<CampaignItem*>(model->rootItem());
    if (!item)
    {
        QMessageBox::information(0, tr("Invalid Campaign Name"), tr("Campaign does not exist!"));
        return;
    }

    QString fileName = item->filePath();
    if (!QFile::exists(fileName))
    {
        onSaveAs(index);
        return;
    }

    if (!FileHandler::save(fileName, item))
    {
        QMessageBox::information(0, tr("Save Failed"), tr("Failed to save campaign!"));
    }

    item->setDirty(false);
}



void TabWidget::onSaveAs(int index)
{
    if (index < 0) index = currentIndex();

    QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), QDir::currentPath(), tr("Xml Files (*.xml)"));

    if (fileName.isEmpty()) return;

    QTreeView* view = qobject_cast<QTreeView*>(widget(index));
    if (!view) return;

    ItemModel* model = qobject_cast<ItemModel*>(view->model());
    if (!model) return;


    CampaignItem* item = qobject_cast<CampaignItem*>(model->rootItem());
    if (!item)
    {
        QMessageBox::information(0, tr("Invalid Campaign Name"), tr("Campaign does not exist!"));
        return;
    }

    if (!FileHandler::save(fileName, item))
    {
        QMessageBox::information(0, tr("Save Failed"), tr("Failed to save campaign!"));
    }

    item->setFilePath(fileName);
    item->setDirty(false);

}



void TabWidget::onClose(int index)
{
    if (index < 0) index = currentIndex();


    QTreeView* v = view(index);
    if (!v) return;

    ItemModel* model = dynamic_cast<ItemModel*>(v->model());
    if (!model || !model->rootItem()) return;

    QString name = model->rootItem()->name();

    CampaignItem* item = CampaignItem::take(name);

    if (!item || !item->isDirty())
    {
        removeTab(index);
        return;
    }


    if (QMessageBox::question(0,
                              tr("Save Changed?"),
                              tr("Campaign has changed. Do you want to save it?"),
                              QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)
    {
        removeTab(index);
        return;
    }

    onSave();
    removeTab(index);
}



void TabWidget::onRefresh()
{
    QTreeView* v = view();
    if (!v) return;

    QHeaderView* header = v->header();
    if (!header) return;

    header->setResizeMode(QHeaderView::ResizeToContents);

    QTimer::singleShot(0, this, SLOT(onRelease()));
}



void TabWidget::onRelease()
{
    QTreeView* v = view();
    QHeaderView* header = v->header();
    if (!header) return;

    header->setResizeMode(QHeaderView::Interactive);
}



void TabWidget::onBeginRename(int index)
{
    if (index < 0) index = currentIndex();

    QTabBar* tab_bar = tabBar();
    if (!tab_bar) return;

    edit_index = index;

    QString name = tabText(index);
    setTabText(index, "");


    QLineEdit* le = qobject_cast<QLineEdit*>(tab_bar->tabButton(edit_index, QTabBar::LeftSide));
    if (!le)
    {
        le = new QLineEdit(name);
        tab_bar->setTabButton(index, QTabBar::LeftSide, le);

    }
    le->show();
    le->setFocus();
    le->selectAll();

    connect(le, SIGNAL(editingFinished()), this, SLOT(onEndRename()));
}



void TabWidget::onEndRename()
{
    int index = edit_index;
    edit_index = -1;
    if (index < 0) return;

    QTabBar* tab_bar = tabBar();
    if (!tab_bar) return;

    QLineEdit* le = qobject_cast<QLineEdit*>(tab_bar->tabButton(index, QTabBar::LeftSide));
    if (!le) return;

    QString name = le->text();

    // remove line edit from tab
    tab_bar->setTabButton(index, QTabBar::LeftSide, 0);
    le->hide();
    le->deleteLater();

    if (name.simplified().isEmpty()) return;

    // set name to
    QTreeView* view = qobject_cast<QTreeView*>(widget(index));
    if (!view) return;

    ItemModel* model = qobject_cast<ItemModel*>(view->model());
    if (!model) return;

    AbstractItem* root = model->rootItem();
    root->setData(AbstractItem::NameData, name);

    setTabText(index, name);
}



QTreeView* TabWidget::view(int index)
{
    if (index < 0) index = currentIndex();

    return qobject_cast<QTreeView*>(widget(index));
}
